package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.domain.Cart;
import org.csu.mypetstore.domain.CartItem;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.service.CartService;
import org.csu.mypetstore.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.annotation.SessionScope;

import javax.servlet.http.HttpServletRequest;
import java.util.Iterator;

@Controller
@SessionScope
@RequestMapping("/cart/")
@SessionAttributes({"cart","account"})
public class CartController {
    @Autowired
    private CatalogService catalogService;
    @Autowired
    private CartService cartService;
    private Cart cart;

    /*  先执行的语句  */
    @GetMapping("viewCart")
    public String viewCart(@SessionAttribute("account") Account account, Model model){
        System.out.println("aaserf       sadf asdf"+"    "+account.getUsername());
        cart= cartService.getCart(account.getUsername());
        System.out.println("aaserf       sadf asdf"+"    "+account.getUsername());
        model.addAttribute("cart",cart);
        return "cart/Cart";
    }
    @GetMapping("addItemToCart")
    public String addItemToCart(String workingItemId, Account account,Cart cart,Model model){
        Item item = catalogService.getItem(workingItemId);
        if(cart.containsItemId(workingItemId)){
            cart.incrementQuantityByItemId(workingItemId);
            cartService.updateCart(account.getUsername(),item.getItemId(),item.getQuantity());
        }else{
            boolean isInStock = catalogService.isItemInStock(workingItemId);
            cart.addItem(item,isInStock);
            cartService.insertCart(account.getUsername(),workingItemId);
        }
        model.addAttribute("cart",cart);
        return "cart/Cart";
    }
    @GetMapping("removeItemFromCart")
    public String removeItemFromCart(String workingItemId, Account account, Cart cart, Model model){
        Item item = cart.removeItemById(workingItemId);
        model.addAttribute("cart",cart);
        cartService.removeCart(account.getUsername(),item.getItemId());
        if(item == null){
            model.addAttribute("msg", "Attempted to remove null CartItem from Cart.");
            return "Error";
        }else{
            return "cart/Cart";
        }
    }
    @PostMapping("updateCartQuantities")
    public String updateCartQuantities(HttpServletRequest request, Account account, Model model){
        Iterator<CartItem> cartItems = cart.getAllCartItems();
        while (cartItems.hasNext()){
            CartItem cartItem = cartItems.next();
            String itemId = cartItem.getItem().getItemId();
            try{
                int quantity = Integer.parseInt(request.getParameter(itemId));
                cart.setQuantityByItemId(itemId,quantity);
                cartService.updateCart(account.getUsername(),itemId,quantity);
                if(quantity < 1){
                    cartItems.remove();
                }
            }catch (Exception e){
            }
        }
        model.addAttribute("cart",cart);
        return "cart/Cart";
    }
}
